package com.xxxx.crm;

import com.alibaba.fastjson.JSON;
import com.xxxx.crm.base.ResultInfo;
import com.xxxx.crm.exceptions.NoLoginException;
import com.xxxx.crm.exceptions.ParamsException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author:王亚栋
 * @Date:2022/12/19 14:56
 * @Version:1.0
 * 全局统一异常处理
 */
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {


    /**
     * 异常处理方法
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @return  JSON数据
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {


        /**
         * 非法请求拦截
         */
        if (ex instanceof NoLoginException){

            ModelAndView modelAndView = new ModelAndView("redirect:/index");

            return modelAndView;
        }

        //设置默认异常处理
        ModelAndView modelAndView = new ModelAndView("error");
        modelAndView.addObject("code",500);
        modelAndView.addObject("msg","系统异常，请重试！");

        //判断handlerMethod
        if (handler instanceof HandlerMethod){

            //类型转换
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            // 获取方法上声明的@ResponseBody注解
            ResponseBody declaredAnnotation = handlerMethod.getMethod().getDeclaredAnnotation(ResponseBody.class);
            //如果声明的注解为空 则返回视图
            if (declaredAnnotation == null) {

                // 返回视图
                if (ex instanceof ParamsException){

                   ParamsException p =  (ParamsException) ex;

                  modelAndView.addObject("code",p.getCode());
                  modelAndView.addObject("msg",p.getMsg());

                }
                return modelAndView;

            }else {

                //返回数据
                ResultInfo resultInfo = new ResultInfo();
                resultInfo.setCode(500);
                resultInfo.setMsg("系统异常，请重试！");

                if (ex instanceof ParamsException){

                    ParamsException p =  (ParamsException) ex;

                    resultInfo.setCode(p.getCode());
                    resultInfo.setMsg(p.getMsg());
                }

                response.setContentType("application/json;charset=UTF-8");

                PrintWriter outer = null;
                try {
                     outer = response.getWriter();

                    String json = JSON.toJSONString(resultInfo);

                    outer.write(json);
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (outer != null){
                        outer.close();
                    }
                }
                return null;
            }
        }
        return modelAndView;
    }
}
